package cn.caishen.report.dao.report;

import cn.caishen.report.common.utils.LbMap;
import cn.caishen.report.domain.report.ReportConfig;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 报表配置持久层
 *
 * @author LB
 * @description 报表配置持久层
 * @create 2022-01-17 15:46:55
 **/
@Mapper
public interface ReportConfigDao extends BaseMapper<ReportConfig> {

    /**
     * 动态报表查询字段列表，用于前端下拉显示
     *
     * @param tableName
     * @param unitId
     * @return
     */
    @Select("select ${id} as id, ${name} as name from ${tableName}")
    List<LbMap> getSelectList(@Param("tableName") String tableName, @Param("unitId") int unitId, @Param("id") String id, @Param("name") String name);


    /**
     * 动态报表查询
     *
     * @param selectSql
     * @param fromSql
     * @param whereSql
     * @param groupBySql
     * @param orderBySql
     * @return
     */
    @Select("<script>" +
            " select ${selectSql} " +

            " from ${fromSql} " +

            " where 1 = 1 " +

            "<if test='whereSql != null and whereSql != &quot;&quot;'>" +
                " and ${whereSql} " +
            "</if>" +

            "<if test='groupBySql != null and groupBySql != &quot;&quot;'>" +
                " group by ${groupBySql} " +
            "</if>" +

            "<if test='orderBySql != null and orderBySql != &quot;&quot;'>" +
                " order by ${orderBySql} " +
            "</if>" +

            "<if test='lastSql != null and lastSql != &quot;&quot;'>" +
                " ${lastSql} " +
            "</if>" +

            "</script>")
    List<LbMap> getReportList(@Param("selectSql") String selectSql, @Param("fromSql") String fromSql, @Param("whereSql") String whereSql,
                                      @Param("groupBySql") String groupBySql, @Param("orderBySql") String orderBySql, @Param("lastSql") String lastSql);

    /**
     * 动态报表查询，外面包裹临时表
     *
     * @param selectSql
     * @param fromSql
     * @param whereSql
     * @param groupBySql
     * @param orderBySql
     * @param selectFields
     * @return
     */
    @Select("<script>" +
            "select " +

            "<if test='groupFields != null and groupFields != &quot;&quot;'>" +
                " ${groupFields} " +
            "</if>" +

            "<if test='selectFields != null and selectFields != &quot;&quot; and (groupFields == null or groupFields == &quot;&quot;)'>" +
                " ${selectFields} " +
            "</if>" +

            " from (" +

            " select ${selectSql} " +

            " from ${fromSql} " +

            " where 1 = 1 " +

            "<if test='whereSql != null and whereSql != &quot;&quot;'>" +
                " and ${whereSql} " +
            "</if>" +

            "<if test='groupBySql != null and groupBySql != &quot;&quot;'>" +
                " GROUP BY ${groupBySql} " +
            "</if>" +

            "<if test='orderBySql != null and orderBySql != &quot;&quot;'>" +
                " ORDER BY ${orderBySql} " +
            "</if>" +

            "<if test='lastSql != null and lastSql != &quot;&quot;'>" +
                " ${lastSql} " +
            "</if>" +

            ") tmp " +

            "<if test='groupFields != null and groupFields != &quot;&quot;'>" +
                " GROUP BY ${groupBySql} " +
            "</if>" +

            "</script>")
    List<LbMap> getTempReportList(@Param("selectSql") String selectSql, @Param("fromSql") String fromSql, @Param("whereSql") String whereSql,
                                  @Param("groupBySql") String groupBySql, @Param("orderBySql") String orderBySql,
                                  @Param("selectFields") String selectFields, @Param("groupFields") String groupFields,
                                  @Param("lastSql") String lastSql);
}
